package com.zh.goods.service.impl;


import com.zh.common.domain.UserDO;
import com.zh.common.utils.ShiroUtils;
import com.zh.common.utils.StringUtil;
import com.zh.goods.dao.GoodsOrderDao;
import com.zh.goods.dao.GoodsOrderStatusDao;
import com.zh.goods.domain.GoodsOrderDO;
import com.zh.goods.domain.GoodsOrderStatusDO;
import com.zh.goods.service.GoodsOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;


@Service
public class GoodsOrderServiceImpl implements GoodsOrderService {

	@Autowired
	private GoodsOrderDao scGoodsOrderReceiptDao;

	@Autowired
	private GoodsOrderStatusDao goodsOrderStatusDao;

	@Override
	public GoodsOrderDO get(String id){
		return scGoodsOrderReceiptDao.get(id);
	}

	@Override
	public List<GoodsOrderDO> list(Map<String, Object> map){
		return scGoodsOrderReceiptDao.list(map);
	}

	@Override
	public int count(Map<String, Object> map){
		return scGoodsOrderReceiptDao.count(map);
	}

	@Override
	public int getCountMonth() {
		return scGoodsOrderReceiptDao.getCountMonth();
	}

	@Override
	public List<GoodsOrderDO> getMonthList(Map<String, Object> map) {
		return scGoodsOrderReceiptDao.getMonthList(map);
	}

	@Override
	public int getSumGoodsOrderReceipt() {
		return scGoodsOrderReceiptDao.getSumGoodsOrderReceipt();
	}

	@Override
	public int getMonthSumGoodsOrderReceipt() {
		return scGoodsOrderReceiptDao.getMonthSumGoodsOrderReceipt();
	}

	@Override
	public int save(GoodsOrderDO scGoodsOrderReceipt){
		return scGoodsOrderReceiptDao.save(scGoodsOrderReceipt);
	}

	@Override
	public int update(GoodsOrderDO scGoodsOrderReceipt){
		return scGoodsOrderReceiptDao.update(scGoodsOrderReceipt);
	}

	@Override
	public int remove(String id){
		return scGoodsOrderReceiptDao.remove(id);
	}

	@Override
	public int batchRemove(String[] ids){
		return scGoodsOrderReceiptDao.batchRemove(ids);
	}

	@Override
	@Transactional
	public int saveOrderAndOrderStatus(GoodsOrderDO scGoodsOrderReceiptDO) {

		UserDO user = ShiroUtils.getUser();
		String orderNo = StringUtil.getOrderNo();
		scGoodsOrderReceiptDO.setId(orderNo);
		scGoodsOrderReceiptDO.setFamilyUserId(user.getUserId());
		scGoodsOrderReceiptDO.setFamiliUserName(user.getName());
		scGoodsOrderReceiptDO.setFamilyUserPhone(user.getMobile());
		scGoodsOrderReceiptDO.setStartTime(new Date());
		scGoodsOrderReceiptDO.setReceiptFlag(0);//待支付

		GoodsOrderStatusDO goodsOrderStatusDO=new GoodsOrderStatusDO();
		goodsOrderStatusDO.setId(StringUtil.generalKeyword());
		goodsOrderStatusDO.setName("下单");
		goodsOrderStatusDO.setDate(new Date());
		goodsOrderStatusDO.setFlag(0);
		goodsOrderStatusDO.setOrederId(orderNo);

		if(scGoodsOrderReceiptDao.save(scGoodsOrderReceiptDO)>0&goodsOrderStatusDao.save(goodsOrderStatusDO)>0){
			return 1;
		}

		return 0;

	}

}
